package controllers

import (
	"fmt"
	beego "github.com/beego/beego/v2/server/web"
	"github.com/jinzhu/gorm"
	"user/models"
)

type UserController struct {
	beego.Controller
}

type ReturnMsg struct {
	Cae  int
	Msg  string
	Data interface{}
}

func (c *UserController) List() {
	var res ReturnMsg

	name := c.GetString("name")
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 5)
	off := (page - 1) / size

	var data []models.User

	models.DB.Debug().Where("name like ?", "%"+name+"%").Offset(off).Limit(size).Find(&data)
	fmt.Println(data)
	res = ReturnMsg{
		Cae:  200,
		Msg:  "查询成功",
		Data: data,
	}
	c.Data["json"] = res
	c.ServeJSON(true)

}

func (c *UserController) Del() {
	var res ReturnMsg
	id, _ := c.GetInt("id")

	data := models.User{
		Model: gorm.Model{ID: uint(id)},
	}
	models.DB.Debug().Delete(&data)
	res = ReturnMsg{
		Cae: 200,
		Msg: "删除成功",
	}
	c.Data["json"] = res
	c.ServeJSON(true)
}
